package com.example.questionnaire.controller;


import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.LoginPageService;
import com.example.questionnaire.service.QuestionnairePageService;
import com.example.questionnaire.service.QuestionnaireSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;



@RestController
@RequestMapping("/QuestionnairePageController")
@Api(tags = "问卷首页")
public class QuestionnairePageController{

    @Autowired
    private QuestionnairePageService questionnairePageService;
    @Autowired
    AppidAndSecretService appidAndSecretService;



    /**
     * 创建问卷名称和问卷说明
     * @param questionnaireNames（问卷名称）
     * @param questionnaireDescription（问卷说明）
     * @return
     * @throws ParseException
     */
    @PostMapping("/calcQuestionnaireName")
    @ApiOperation(value = "创建问卷名称和问卷说明", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse calcQuestionnaireName(@RequestParam String questionnaireNames, @RequestParam String questionnaireDescription) throws ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
       // String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJvRFg2UTVicnUwNVRiU1R6Y2VWRm8xYmtEWll3IiwiaWF0IjoxNjkyNjg1OTg4LCJleHAiOjE2OTI3NzIzODh9.KJULR7vDu1srMRdO_Li1AL7KAXSB8hjX4WD6e2cQf74";
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        return ServerResponse.createBySuccess(questionnairePageService.calcQuestionnaireName(token, questionnaireNames, questionnaireDescription));
    }

    /**
     * 修改问卷名称和问卷说明
     * @param formid（问卷id）
     * @param questionnaireName（问卷名称）
     * @param questionnaireDescription（问卷说明）
     */
    @PostMapping("/updateAddQuestionnaireDescription")
    @ApiOperation(value = "修改问卷名称和问卷说明", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse updateAddQuestionnaireDescription(@RequestParam String formid, @RequestParam String questionnaireName,@RequestParam String questionnaireDescription) throws RException, ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");

        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

        boolean b = questionnairePageService.updateAddQuestionnaireDescription(formid, questionnaireName, questionnaireDescription);
        if (b){
            return ServerResponse.createBySuccess("修改成功");
        }
        return ServerResponse.createByError("修改失败");

    }

    /**
     * 获取所有问卷名称

     */
    @GetMapping("/prepare")
    @ApiOperation(value = "获取所有问卷名称", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse prepare() throws RException, ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

       // String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJvRFg2UTVicnUwNVRiU1R6Y2VWRm8xYmtEWll3IiwiaWF0IjoxNjkyNjg1OTg4LCJleHAiOjE2OTI3NzIzODh9.KJULR7vDu1srMRdO_Li1AL7KAXSB8hjX4WD6e2cQf74";
        return ServerResponse.createBySuccess(questionnairePageService.getQuestionnaireNames(token));

    }

    @GetMapping("/getQuestionnaireName")
    @ApiOperation(value = "获取单个问卷名称", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse getQuestionnaireName(@RequestParam String formid){

        return ServerResponse.createBySuccess(questionnairePageService.getQuestionnaireName(formid));

    }

    /**
     * 删除问卷
     * @param formid（问卷id）
     */
    @PostMapping("/deleteQuestionnaire")
    @ApiOperation(value = "删除问卷", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse deleteQuestionnaire(@RequestParam String formid) throws ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
       // String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJvRFg2UTVicnUwNVRiU1R6Y2VWRm8xYmtEWll3IiwiaWF0IjoxNjkyNjg1OTg4LCJleHAiOjE2OTI3NzIzODh9.KJULR7vDu1srMRdO_Li1AL7KAXSB8hjX4WD6e2cQf74";
        boolean s = questionnairePageService.deleteQuestionnaire(formid, token);
        if (s){
            return ServerResponse.createBySuccess("删除成功");
        }
        return ServerResponse.createByError("删除失败");
    }

    /**
     *
     * @param formid（问卷id）
     * @param status（问卷发布状态）
     * @return
     * @throws ParseException
     */
    @PostMapping("/QuestionnaireRelease")
    @ApiOperation(value = "问卷发布状态", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse QuestionnaireRelease(@RequestParam String formid, @RequestParam String status) throws ParseException {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

        boolean b = questionnairePageService.QuestionnaireRelease(formid, status);
        if (b) {
            return ServerResponse.createBySuccess("修改成功");
        }
        return ServerResponse.createByError("修改失败");
    }


    /**
     *
     * @param formid（问卷id）
     */
    @GetMapping("/getQuestionnaireStatus")
    @ApiOperation(value = "获取问卷发布状态", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse getQuestionnaireStatus(@RequestParam String formid) {


        return ServerResponse.createBySuccess(questionnairePageService.GetQuestionnaireStatus(formid));

    }
}
